package com.myk.game.gobangchess.rooms.oldrooms;

import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.constants.BoardStyleData;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.signs.AroundSign;
import com.myk.game.gobangchess.signs.TextSign;
import com.myk.game.gobangchess.utils.GridsUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * 666五子棋
 */
public class GoBangVariant2Room extends Room {
    public GoBangVariant2Room(String roomName) {
        super(roomName, "\t\t666五子棋", "五子棋变体2", BoardStyleData.getGoBangStyle(15, 15), true, true);
    }
    private int sixPoint[][]={{0,0},{0,0},{0,0}};
    @Override
    public String getRuleText() {
        return "一方选3个6连格，\n" +
                "另一方选执黑白）\n" +
                "黑一天元\n"+
                "连珠有6连格则需6连，否则5连胜\n" +
                "终盘时占据场格多者胜";
    }
    @Override
    protected void onStartPrepare() {
        noticeBlackMove();
        //List<Integer> ids = new ArrayList<>();
        gameTip="设置三个6连点";

    }

    @Override
    public void doRule(int row, int col, int value) {
        if (step==0){
            signList.add(new TextSign(getIndex(row, col),"6","#ff0000ff"));
            sixPoint[0][0] = row;
            sixPoint[0][1] = col;
            stepAdd();
            noticeBlackMove();
            return;
        }else if (step == 1) {
            if (sixPoint[0][0] == row && sixPoint[0][1] == col) {
                return;
            } else {
                sixPoint[1][0] = row;
                sixPoint[1][1] = col;
                signList.add(new TextSign(getIndex(row, col),"6","#ff0000ff"));
                stepAdd();
                noticeBlackMove();
            }
        }else if (step==2){
            if ((sixPoint[0][0] == row && sixPoint[0][1] == col) ||(sixPoint[1][0] == row && sixPoint[1][1] == col)){
                return;
            }else{
                sixPoint[2][0] = row;
                sixPoint[2][1] = col;
                signList.add(new TextSign(getIndex(row, col),"6","#ff0000ff"));
                stepAdd();
                noticeWhiteMove();
            }
        }else if (step==3){
            gameTip = "点天元为黑方，点其他格为白方";

            if (row==7&&col==7){
                curGrids[7][7]=2;
                gameTip ="-1";
                stepAdd();
                noticeBlackMove();
                return;
            }else{
                curGrids[7][7]=1;
                curGrids[row][col]=2;
                gameTip ="-1";
                stepAdd();
                noticeBlackMove();
                return;
            }
        } else if (curGrids[row][col] == 0) { //只能落在空位置
            signList.clear();
            signList.add(new TextSign(getIndex(sixPoint[0][0], sixPoint[0][1]),"6","#ff0000ff"));
            signList.add(new TextSign(getIndex(sixPoint[1][0], sixPoint[1][1]),"6","#ff0000ff"));
            signList.add(new TextSign(getIndex(sixPoint[2][0], sixPoint[2][1]),"6","#ff0000ff"));
            List<Integer> ids = new ArrayList<>();
            ids.add(getIndex(row, col));
            signList.add(new AroundSign(ids, "#00000000", "#6600FF00", 1.2f));
            curGrids[row][col] = value; //实际落子
            stepAdd();
            ; //步数统计加增加

            //黑方落子时
            if (value == 1) {
                if (checkFormation(row,col,1) > 0) {//黑方连5成功
                    doOver(WinMode.BLACK_WIN, player_black.getNickNameOrTemp() + "执黑棋战胜" + player_white.getNickNameOrTemp());
                } else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);
                    if (blankNum == 0) {//没有胜负也没有空子了算战平
                        doOver(WinMode.BOTH_DRAW, player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    } else {
                        noticeWhiteMove();
                    }
                }
            }

            //白方落子时
            else if (value == 2) {
                if (checkFormation(row,col,2) > 0) {
                    doOver(WinMode.WHITE_WIN, player_white.getNickNameOrTemp() + "执白棋战胜" + player_black.getNickNameOrTemp());
                } else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);  //没有胜负也没有空子了算战平
                    if (blankNum == 0) {
                        doOver(WinMode.BOTH_DRAW, player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    } else {
                        noticeBlackMove();
                    }
                }
            }

        }
    }

    //获取成5的次数
    private int checkFormation(int row,int col,int value) {//0未连珠，1成功连珠并获胜(Boolean用起来麻烦)
        int consecutiveNumber = 6;
        int ifHaveSixPoint = 1;//1表示无，0有
        int rows = curGrids.length-1;
        int cols = curGrids[0].length-1;
        int formationCount = 1;
        //左下到右上
        int left = Math.min(Math.min(rows-row,col),consecutiveNumber-1);
        int right = Math.min(Math.min(row,cols-col),consecutiveNumber-1);
        for (int i = 1; i <= left; i++) {
            if (curGrids[row+i][col-i] == value){
                formationCount++;
                if ((sixPoint[0][0] == row+i &&sixPoint[0][1] == col-i)||(sixPoint[1][0] == row+i &&sixPoint[1][1] == col-i)||(sixPoint[2][0] == row+i &&sixPoint[2][1] == col-i)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        for (int i = 1; i <= right; i++) {
            if (curGrids[row-i][col+i] == value){
                formationCount++;
                if ((sixPoint[0][0] == row-i &&sixPoint[0][1] == col+i)||(sixPoint[1][0] == row-i &&sixPoint[1][1] == col+i)||(sixPoint[2][0] == row-i &&sixPoint[2][1] == col+i)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        if (formationCount >= consecutiveNumber-ifHaveSixPoint) {
            return 1;
        }else{
            formationCount = 1;
            ifHaveSixPoint=1;
        }
        //水平方向
        left = Math.max(0,col-consecutiveNumber+1); //左侧（竖直时定义为上）
        right = Math.min(col+consecutiveNumber-1,cols); //右侧(竖直时定义为下)
        for (int i = col-1;i>=left;i--) {
            if (curGrids[row][i] == value) {
                formationCount++;
                if ((sixPoint[0][0] == row &&sixPoint[0][1] == i)||(sixPoint[1][0] == row &&sixPoint[1][1] == i)||(sixPoint[2][0] == row &&sixPoint[2][1] == i)){
                    ifHaveSixPoint = 0;
                }
            } else {
                break;
            }
        }
        for (int i = col+1;i<=right;i++){
            if (curGrids[row][i] == value){
                formationCount++;
                if ((sixPoint[0][0] == row &&sixPoint[0][1] == i)||(sixPoint[1][0] == row &&sixPoint[1][1] == i)||(sixPoint[2][0] == row &&sixPoint[2][1] == i)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        if (formationCount >= consecutiveNumber-ifHaveSixPoint) {
            return 1;
        }else{
            formationCount = 1;
            ifHaveSixPoint=1;
        }
        //左上到右下
        left = Math.min(Math.min(row,col),consecutiveNumber-1);
        right = Math.min(Math.min(rows-row,cols-col),consecutiveNumber-1);
        for (int i = 1;i<=left;i++){
            if (curGrids[row-i][col-i] == value){
                formationCount++;
                if ((sixPoint[0][0] == row-i &&sixPoint[0][1] == col+i)||(sixPoint[1][0] == row-i &&sixPoint[1][1] == col+i)||(sixPoint[2][0] == row-i &&sixPoint[2][1] == col+i)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        for (int i = 1;i<=right;i++){
            if (curGrids[row+i][col+i] == value){
                formationCount++;
                if ((sixPoint[0][0] == row+i && sixPoint[0][1] == col+i)||(sixPoint[1][0] == row+i &&sixPoint[1][1] == col+i)||(sixPoint[2][0] == row+i &&sixPoint[2][1] == col+i)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        if (formationCount >= consecutiveNumber-ifHaveSixPoint) {
            return 1;
        }else{
            formationCount = 1;
            ifHaveSixPoint = 1;
        }
        //竖直
        left = Math.min(row,consecutiveNumber-1);
        right = Math.min(rows-row,consecutiveNumber-1);
        for (int i = 1;i<=left;i++) {
            if (curGrids[row-i][col] == value){
                formationCount++;
                if ((sixPoint[0][0] == row-i &&sixPoint[0][1] == col)||(sixPoint[1][0] == row-i &&sixPoint[1][1] == col)||(sixPoint[2][0] == row-i &&sixPoint[2][1] == col)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        for (int i = 1;i<=right;i++){
            if (curGrids[row+i][col] == value){
                formationCount++;
                if ((sixPoint[0][0] == row+i &&sixPoint[0][1] == col)||(sixPoint[1][0] == row+i &&sixPoint[1][1] == col)||(sixPoint[2][0] == row+i &&sixPoint[2][1] == col)){
                    ifHaveSixPoint = 0;
                }
            }else{
                break;
            }
        }
        if (formationCount >= consecutiveNumber-ifHaveSixPoint) {
            return 1;
        }
        return 0;
    }

}
